<?php
namespace app\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\console\input\Argument;
use think\console\input\Option;
use Workerman\Worker;

class TcpServer extends Command
{
    protected function configure()
    {
        $this->setName('tcp:server')
             ->setDescription('启动TCP服务器')
             ->addArgument('action', Argument::OPTIONAL, 'start|stop|restart|status', 'start')
             ->addOption('host', null, Option::VALUE_OPTIONAL, '监听地址', '0.0.0.0')
             ->addOption('port', null, Option::VALUE_OPTIONAL, '监听端口', 2346)
             ->addOption('processes', null, Option::VALUE_OPTIONAL, '进程数量', 4);
    }
    
    protected function execute(Input $input, Output $output)
    {
        $action = $input->getArgument('action');
        $host = $input->getOption('host');
        $port = $input->getOption('port');
        $processes = $input->getOption('processes');
        
        $output->writeln("🚀 ThinkPHP TCP服务器命令控制");
        $output->writeln("📍 地址: {$host}:{$port}");
        $output->writeln("👥 进程: {$processes}");
        $output->writeln("🎯 动作: {$action}");
        
        switch ($action) {
            case 'start':
                $this->startServer($host, $port, $processes, $output);
                break;
                
            case 'stop':
                $this->stopServer($output);
                break;
                
            case 'restart':
                $this->restartServer($host, $port, $processes, $output);
                break;
                
            case 'status':
                $this->serverStatus($output);
                break;
                
            default:
                $output->writeln('❌ 未知动作，可用动作: start|stop|restart|status');
        }
    }
    
    /**
     * 启动TCP服务器
     */
    protected function startServer($host, $port, $processes, Output $output)
    {
        $output->writeln('🔧 启动TCP服务器...');
        
        // 创建TCP Worker
        $tcpWorker = new Worker("tcp://{$host}:{$port}");
        $tcpWorker->count = $processes;
        $tcpWorker->name = 'ThinkPHP-TCP-Server';
        
        // 设置事件回调
        $tcpWorker->onWorkerStart = function($worker) use ($output) {
            $output->writeln("✅ Worker进程启动: PID=" . posix_getpid());
        };
        
        $tcpWorker->onConnect = function($connection) use ($output) {
            $output->writeln("✅ 客户端连接: {$connection->getRemoteIp()}");
        };
        
        $tcpWorker->onMessage = function($connection, $data) use ($output) {
            $output->writeln("📨 收到数据: " . substr($data, 0, 50) . "...");
            $connection->send("Server received: " . $data);
        };
        
        $output->writeln("🎉 TCP服务器启动成功!");
        $output->writeln("⏰ 启动时间: " . date('Y-m-d H:i:s'));
        
        // 运行Worker
        Worker::runAll();
    }
    
    protected function stopServer(Output $output)
    {
        $output->writeln('🛑 停止TCP服务器...');
        // 实现停止逻辑
    }
    
    protected function restartServer($host, $port, $processes, Output $output)
    {
        $output->writeln('🔄 重启TCP服务器...');
        // 实现重启逻辑
    }
    
    protected function serverStatus(Output $output)
    {
        $output->writeln('📊 服务器状态检查...');
        // 实现状态检查逻辑
    }
}